package net.maku.needy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import net.maku.framework.common.utils.PageResult;
import net.maku.framework.mybatis.service.impl.BaseServiceImpl;
import net.maku.needy.convert.NeedyMemberFamilyConvert;
import net.maku.needy.entity.NeedyMemberFamilyEntity;
import net.maku.needy.query.NeedyMemberFamilyQuery;
import net.maku.needy.vo.NeedyMemberFamilyVO;
import net.maku.needy.dao.NeedyMemberFamilyDao;
import net.maku.needy.service.NeedyMemberFamilyService;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 困难职工家庭成员
 *
 * @author 阿沐 babamu@126.com
 * @since 1.0.0 2025-03-20
 */
@Service
@AllArgsConstructor
public class NeedyMemberFamilyServiceImpl extends BaseServiceImpl<NeedyMemberFamilyDao, NeedyMemberFamilyEntity> implements NeedyMemberFamilyService {

    @Override
    public PageResult<NeedyMemberFamilyVO> page(NeedyMemberFamilyQuery query) {
        IPage<NeedyMemberFamilyEntity> page = baseMapper.selectPage(getPage(query), getWrapper(query));

        return new PageResult<>(NeedyMemberFamilyConvert.INSTANCE.convertList(page.getRecords()), page.getTotal());
    }

    private LambdaQueryWrapper<NeedyMemberFamilyEntity> getWrapper(NeedyMemberFamilyQuery query){
        LambdaQueryWrapper<NeedyMemberFamilyEntity> wrapper = Wrappers.lambdaQuery();
        return wrapper;
    }

    @Override
    public void save(NeedyMemberFamilyVO vo) {
        NeedyMemberFamilyEntity entity = NeedyMemberFamilyConvert.INSTANCE.convert(vo);

        baseMapper.insert(entity);
    }

    @Override
    public void update(NeedyMemberFamilyVO vo) {
        NeedyMemberFamilyEntity entity = NeedyMemberFamilyConvert.INSTANCE.convert(vo);

        updateById(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(List<Long> idList) {
        removeByIds(idList);
    }

    @Override
    public List<NeedyMemberFamilyEntity> selectMemberFamilyList(Long memberId) {
        QueryWrapper<NeedyMemberFamilyEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("member_id",memberId);
        return baseMapper.selectList(queryWrapper);
    }

}